package com.speedchina.oauth2_permission.web.sys.api_v1;

import com.speedchina.framework.base.QueryModel;
import com.speedchina.framework.helper.Assert;
import com.speedchina.framework.model.R;
import com.speedchina.framework.validator.ValidatorHelper;
import com.speedchina.framework.validator.group.AddGroup;
import com.speedchina.framework.validator.group.UpdateGroup;
import com.speedchina.oauth2_permission.aspect.SysLog;
import com.speedchina.permission.base.constant.SysConstant;
import com.speedchina.permission.base.i18n.I18nService;
import com.speedchina.permission.domain.sys.po.ResourcePo;
import com.speedchina.permission.service.sys.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 接口
 *
 * @author winter
 * @date 2021-04-07 15:19:42
 */
@RestController
@RequestMapping("/sys/resource")
@Api(description = "菜单管理 接口")
public class ResourceController {

    @Autowired
    private I18nService i18nService;

    @Autowired
    private ResourceService service;

    @ApiOperation("分页查询菜单")
    @GetMapping
    @SysLog("分页查询-菜单")
    //@PreAuthorize("hasAuthority('sys:menu:list')")
    public R query(Integer pageNo, Integer pageSize, ResourcePo params) {
        return R.page(service.queryPage(new QueryModel<>(pageNo, pageSize, params)));
    }

    @ApiOperation("查询菜单树")
    @GetMapping("trees")
    @SysLog("查询菜单树-")
//     //@PreAuthorize("hasAuthority('sys:menu:trees')")
    public R trees(ResourcePo params) {
        return R.data(service.trees(params));
    }

    @ApiOperation("详情查询")
    @GetMapping("/get")
    @SysLog("详情查询-菜单")
    //@PreAuthorize("hasAuthority('sys:menu:info')")
    public R get(ResourcePo params) {
        return R.data(service.get(params));
    }

    @ApiOperation("新增菜单")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "", value = "", dataType = "String", paramType = "path")
    })
    @PostMapping
    @SysLog("新增-菜单")
    @PreAuthorize("hasAuthority('sys:menu:add')")
    public R add(@RequestBody ResourcePo entity) {
        ValidatorHelper.validateEntity(entity, AddGroup.class);
        return R.data(service.saveOrUpdate4AutoGenId(entity));
    }

    @ApiOperation("修改菜单")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path")
    })
    @PutMapping
    @SysLog("修改-菜单")
    @PreAuthorize("hasAuthority('sys:menu:update')")
    public R update(@RequestBody ResourcePo entity) {
        ValidatorHelper.validateEntity(entity, UpdateGroup.class);
        return R.data(service.updateById(entity));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/{ids}")
    @SysLog("批量删除-菜单")
    @PreAuthorize("hasAuthority('sys:menu:delete')")
    public R delete(@PathVariable("ids") String strIds) {
        Assert.isNull(strIds, i18nService.getMessage("DELETE_CONDITION_ILLEGAL"));
        List<Serializable> ids = new ArrayList<>();
        for (String id : strIds.split(SysConstant.SPLIT_REGEX)) {
            if (StringUtils.isNotBlank(id)) {
                ids.add(id);
            }
        }
        Assert.isTrue(ids.isEmpty(), i18nService.getMessage("DELETE_CONDITION_ILLEGAL"));
        return R.data(service.deleteLinkedBatch(ids));
    }

}